<script setup lang="ts">
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import axios from 'axios';

const phone = ref('');
const code = ref('');
const agree = ref(false);
const codeSending = ref(false);
const countdown = ref(60);
const timer = ref<number | null>(null);
const phoneError = ref('');
const codeError = ref('');
const router = useRouter();

// 动态效果状态
const phoneIsFocused = ref(false);
const codeIsFocused = ref(false);
const switchBtnHover = ref(false);

const isPhoneValid = () => /^1[3-9]\d{9}$/.test(phone.value);
const canLogin = () => isPhoneValid() && code.value.length === 4 && agree.value;

function sendCode() {
  phoneError.value = '';
  if (!isPhoneValid()) {
    phoneError.value = '请输入有效的手机号';
    return;
  }
  codeSending.value = true;

  // 调用后端发送验证码API
  axios.get(`/user/sendCode?phone=${phone.value}`)
    .then(response => {
      console.log('验证码发送成功:', response.data);
      // 启动倒计时
      countdown.value = 60;
      timer.value = setInterval(() => {
        countdown.value--;
        if (countdown.value <= 0) {
          clearInterval(timer.value!);
          codeSending.value = false;
        }
      }, 1000);
    })
    .catch(error => {
      console.error('验证码发送失败:', error);
      phoneError.value = '验证码发送失败，请稍后重试';
      codeSending.value = false;
    });
}

function handleLogin() {
  phoneError.value = '';
  codeError.value = '';
  if (!isPhoneValid()) {
    phoneError.value = '请输入有效的手机号';
    return;
  }
  // 修改为4位验证码
  if (code.value.length !== 4) {
    codeError.value = '请输入4位验证码';
    return;
  }
  if (!agree.value) {
    codeError.value = '请同意用户协议';
    return;
  }

  // 调用后端API进行登录
  axios.post('/user/login', {
    phone: phone.value,
    code: code.value
  })
    .then(response => {
      if (response.data.code === 200) {
        // 登录成功，存储token和用户信息
        localStorage.setItem('token', response.data.data.token);
        localStorage.setItem('userInfo', JSON.stringify(response.data.data.user));
        // 跳转到主页
        router.push('/main');
      } else {
        // 登录失败，显示错误信息
        codeError.value = response.data.msg || '验证码错误';
      }
    })
    .catch(error => {
      console.error('登录失败:', error);
      codeError.value = '登录失败，请稍后重试';
    });
}

const goPassLogin = () => {
  router.push('/passlogin');
};

function validatePhone() {
  if (!phone.value) {
    phoneError.value = '请输入手机号';
  } else if (!isPhoneValid()) {
    phoneError.value = '请输入有效的手机号';
  } else {
    phoneError.value = '';
  }
}

function validateCode() {
  if (!code.value) {
    codeError.value = '请输入验证码';
  } else if (code.value.length !== 4) {
    codeError.value = '请输入4位验证码';
  } else {
    codeError.value = '';
  }
}
</script>

<template>
  <div class="login-bg">
    <div class="login-card">
      <h2 class="login-title">THINK · 智慧校园<br />后台管理系统</h2>

      <div class="input-group" :class="{ 'input-focus': phoneIsFocused }">
        <div class="input-label">请输入手机号</div>
        <input v-model="phone" placeholder="请输入11位手机号" class="custom-input" maxlength="11" type="text"
          @focus="phoneIsFocused = true" @blur="() => { phoneIsFocused = false; validatePhone(); }" />
        <div class="input-underline"></div>
        <div class="input-active-underline" :class="{ 'underline-active': phoneIsFocused }"></div>
      </div>
      <div class="error" v-if="phoneError">{{ phoneError }}</div>

      <div class="input-group" :class="{ 'input-focus': codeIsFocused }">
        <div class="input-label">验证码</div>
        <div class="code-row">
          <input v-model="code" placeholder="请输入4位验证码" class="custom-input code-input" maxlength="4" type="text"
            @focus="codeIsFocused = true" @blur="() => { codeIsFocused = false; validateCode(); }" />
          <el-button type="text" class="send-code" :disabled="codeSending || !isPhoneValid()" @click="sendCode"
            :class="{ 'send-code-pulse': !codeSending && isPhoneValid() }">
            {{ codeSending ? `${countdown}秒后重试` : '发送验证码' }}
          </el-button>
        </div>
        <div class="input-underline"></div>
        <div class="input-active-underline" :class="{ 'underline-active': codeIsFocused }"></div>
      </div>
      <div class="error" v-if="codeError">{{ codeError }}</div>

      <div class="login-agree">
        <el-checkbox v-model="agree" class="custom-checkbox" />
        <span>我已阅读并同意 <a href="#" style="color:#1da1f2">《THINK智慧校园用户协议》</a></span>
      </div>

      <div class="button-group">
        <el-button type="primary" class="login-btn" :disabled="!canLogin()" @click="handleLogin"
          :class="{ 'btn-active': canLogin() }">
          登录
        </el-button>
        <el-button class="switch-btn" @click="goPassLogin" :class="{ 'switch-btn-hover': switchBtnHover }"
          @mouseenter="switchBtnHover = true" @mouseleave="switchBtnHover = false">
          切换密码登录
        </el-button>
      </div>
    </div>
    <div class="login-circle"></div>
  </div>
</template>

<style scoped>
.login-bg {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background: #25b4fd;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 0;
}

.login-card {
  background: #fff;
  border-radius: 32px;
  box-shadow: 0 12px 48px rgba(0, 0, 0, 0.12);
  padding: 64px 56px 48px 56px;
  width: 540px;
  max-width: 96vw;
  display: flex;
  flex-direction: column;
  align-items: center;
  z-index: 2;
  transform: translateY(0);
  animation: fadeIn 0.5s ease-out;
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(20px);
  }

  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.login-title {
  color: #3bb6fd;
  font-size: 40px;
  font-weight: 900;
  text-align: center;
  margin-bottom: 56px;
  line-height: 1.2;
}

.input-group {
  width: 100%;
  margin-bottom: 36px;
  position: relative;
  transition: all 0.3s ease;
}

.input-focus {
  transform: translateY(-2px);
}

.input-label {
  color: #bfbfbf;
  font-size: 20px;
  font-weight: bold;
  margin-bottom: 10px;
  transition: color 0.3s ease;
}

.input-focus .input-label {
  color: #3bb6fd;
}

.custom-input {
  width: 100%;
  border: none;
  outline: none;
  font-size: 22px;
  font-weight: normal;
  padding: 8px 0;
  transition: all 0.3s ease;
  background: transparent;
}

.input-underline {
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 1px;
  background-color: #e0e0e0;
  transition: all 0.3s ease;
}

.input-active-underline {
  position: absolute;
  bottom: 0;
  left: 0;
  width: 0;
  height: 2px;
  background-color: #3bb6fd;
  transition: all 0.3s ease;
}

.underline-active {
  width: 100%;
}

.code-row {
  display: flex;
  align-items: center;
  width: 100%;
}

.send-code {
  color: #3bb6fd;
  font-size: 18px;
  font-weight: bold;
  padding: 0;
  margin-left: 8px;
  white-space: nowrap;
  transition: all 0.3s ease;
}

.send-code:hover:not(:disabled) {
  transform: scale(1.05);
  color: #25a4ed;
}

.send-code-pulse {
  animation: pulse 1.5s infinite;
}

@keyframes pulse {
  0% {
    transform: scale(1);
  }

  50% {
    transform: scale(1.05);
  }

  100% {
    transform: scale(1);
  }
}

.send-code:disabled {
  color: #b0dfff;
  cursor: not-allowed;
}

.login-agree {
  display: flex;
  align-items: center;
  margin-bottom: 36px;
  width: 100%;
  transition: all 0.3s ease;
}

.login-agree:hover {
  transform: translateX(5px);
}

.custom-checkbox :deep(.el-checkbox__input.is-checked .el-checkbox__inner) {
  background-color: #3bb6fd;
  border-color: #3bb6fd;
}

.button-group {
  display: flex;
  flex-direction: column;
  width: 100%;
  gap: 20px;
}

.login-btn {
  width: 100%;
  height: 64px;
  border-radius: 32px;
  font-size: 26px;
  font-weight: bold;
  background: linear-gradient(90deg, #25b4fd 0%, #3bb6fd 100%);
  box-shadow: 0 6px 24px rgba(59, 182, 253, 0.18);
  border: none;
  letter-spacing: 2px;
  transition: all 0.3s ease;
  position: relative;
  overflow: hidden;
}

.login-btn:not(:disabled):hover {
  transform: translateY(-3px);
  box-shadow: 0 10px 24px rgba(59, 182, 253, 0.25);
}

.login-btn:not(:disabled):active {
  transform: translateY(1px);
}

.btn-active {
  animation: buttonGlow 2s infinite;
}

@keyframes buttonGlow {
  0% {
    box-shadow: 0 6px 24px rgba(59, 182, 253, 0.18);
  }

  50% {
    box-shadow: 0 6px 30px rgba(59, 182, 253, 0.4);
  }

  100% {
    box-shadow: 0 6px 24px rgba(59, 182, 253, 0.18);
  }
}

.login-btn:disabled {
  background: #b0dfff;
  opacity: 0.7;
}

.switch-btn {
  width: 100%;
  height: 64px;
  border-radius: 32px;
  background: #f5f5f5;
  color: #b0b0b0;
  border: none;
  font-size: 26px;
  font-weight: bold;
  box-shadow: 0 6px 24px rgba(59, 182, 253, 0.10);
  letter-spacing: 2px;
  transition: all 0.3s ease;
}

.switch-btn:hover {
  transform: translateY(-2px);
}

.switch-btn-hover {
  background: #f0f0f0;
  color: #909090;
}

.login-circle {
  position: absolute;
  left: 80px;
  top: 120px;
  width: 220px;
  height: 220px;
  border-radius: 50%;
  border: 16px solid rgba(255, 255, 255, 0.2);
  box-sizing: border-box;
  z-index: 1;
  animation: float 6s ease-in-out infinite;
}

@keyframes float {
  0% {
    transform: translateY(0px);
  }

  50% {
    transform: translateY(-20px);
  }

  100% {
    transform: translateY(0px);
  }
}

.error {
  color: #ff4d4f;
  font-size: 15px;
  text-align: left;
  margin-top: -28px;
  margin-bottom: 16px;
  width: 100%;
}
</style>
